/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : Usart.c                                               *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : This file provides the serial routine related         *
 *                     configuration functions, including                     *
 *                      + serial port send                                    *
 *                      + serial port asynchronous configuration              *
 *                      + serial port synchronization configuration           *
 *                      + serial receiving interrupt enable                   *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                 **
******************************************************************************/
#include "system_init.h"
#include "Usart.h"
#include "Usart_Config.h"
/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void Usart_Gpio_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure PF5 remap mode, as TX pin, it should be pullup */
	/* Configure PF5 remap mode */
	GPIO_InitStructure.m_Mode = GPIO_MODE_RMP;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_PULLUP;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_5;
	GPIO_Configuration(GPIOF_SFR , &GPIO_InitStructure);
	/* Configure PF5 remap function to AF6 */
	GPIO_Pin_RMP_Config(GPIOF_SFR, GPIO_Pin_Num_5, GPIO_RMP_PG1_AF6);
}

/**
 *  @brief :USART sends data and waits for completion
 *  @param in :
 *              USARTx :USARTx : A pointer to the USART memory structure
 *                  	 with a value of USART0_SFR/USART1_SFR/USART2_SFR/USART5_SFR
 *  			      Databuf: A pointer to the sending data
 *  			      Length : Length of transmission
 *  @param out :None
 *  @retval :None
 */

void USART_Send(USART_SFRmap *USARTx, Usart_Sdu_Type *Usart_Sdu)
{
  for (uint32_t Send_data_count = 0; Send_data_count < Usart_Sdu->Length; Send_data_count++)
  {
    USART_SendData(USARTx, Usart_Sdu->Data[Send_data_count]);
    while (!USART_Get_Transmitter_Empty_Flag(USARTx))
      ;
  }
}

/**
 *  @brief :Serial asynchronous full duplex configuration(Default 8 bit transceiver enabled  Full duplex 9600)
 *  @param in :	
 * 				USARTx : A pointer to the USART memory structure
 *                  	 with a value of USART0_SFR/USART1_SFR/USART2_SFR/USART5_SFR
 *  @param out :None
 *  @retval :None
 */
void USART_Mode_Config(USART_SFRmap *USARTx)
{
  USART_InitTypeDef USART_InitStructure;
  USART_Struct_Init(&USART_InitStructure);
  /* Set Usart To Async Mode */
  USART_InitStructure.m_Mode = USART_MODE_FULLDUPLEXASY;
  USART_InitStructure.m_TransferDir = USART_DIRECTION_FULL_DUPLEX;
  USART_InitStructure.m_WordLength = USART_WORDLENGTH_8B;
  USART_InitStructure.m_StopBits = USART_STOPBITS_1;

  /*  Set baudrate to 115200 */
  USART_InitStructure.m_BaudRateBRCKS = USART_CLK_HFCLK;
	/** Use 16M clock as an example to list the following baud rates 
	 * 	4800    z:208    x:0    y:0
	 * 	9600    z:104    x:0    y:0
	 * 	19200   z:52     x:0    y:0
	 * 	115200  z:8      x:1    y:13
	*/
	/* Integer part z, get value range is 0 ~ 0xFFFF */
	USART_InitStructure.m_BaudRateInteger = 8;
	/* Numerator part x, get value range is 0 ~ 0x0f */
	USART_InitStructure.m_BaudRateNumerator = 1;
	/* Denominator part y, get value range is 0 ~ 0x0f */
	USART_InitStructure.m_BaudRateDenominator = 13;
	/* Reset and enable USARTx */
	USART_Reset(USARTx);                                      
	USART_Configuration(USARTx,&USART_InitStructure);
	/* Enable UART moudle */
  	USART_RESHD_Enable(USARTx, TRUE);
  	USART_Transmit_Data_Enable(USARTx, TRUE);
  	USART_Cmd(USARTx, TRUE);
}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
void USART_Int_Config(USART_SFRmap *USARTx, InterruptIndex Peripheral)
{
  USART_Clear_Transmit_BUFR_INT_Flag(USARTx);
  USART_RDR_INT_Enable(USARTx, TRUE);
  USART_TFE_INT_Enable(USARTx, TRUE);
  INT_Interrupt_Enable(Peripheral, TRUE);
}
